

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/notebook.ico">
  <link rel="icon" href="/img/notebook.ico">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
  <title>Java OOM定位处理 - xulinglin</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/monokai-sublime.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="/css/diy/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 30vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>笑语</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                目录
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/img_1.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.5)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="Java OOM定位处理">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-06-04 11:15" pubdate>
        2021年6月4日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      446 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      6
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">Java OOM定位处理</h1>
            
            <div class="markdown-body">
              <p class="note note-info">
本文针对处理 <span class="label label-primary">OOM</span> 的定位问题,查询以及解决问题。
</p>

<h2 id="1-常见的几种方式"><a href="#1-常见的几种方式" class="headerlink" title="1. 常见的几种方式"></a>1. 常见的几种方式</h2><h3 id="1-1-GC-overhead-limit-exceeded"><a href="#1-1-GC-overhead-limit-exceeded" class="headerlink" title="1.1. GC overhead limit exceeded"></a>1.1. GC overhead limit exceeded</h3><div class="hljs code-wrapper"><pre><code class="hljs Java">Java.lang.OutOfMemeoryError:GC overhead limit exceeded</code></pre></div>

<p class="note note-danger">
即程序在垃圾回收上花费了98%的时间，却收集不回2%的空间，通常这样的异常伴随着CPU的冲高。不同的垃圾回收器 OOM 的异常错误不同。
</p>

<h3 id="1-2-StackOverflowError-栈溢出"><a href="#1-2-StackOverflowError-栈溢出" class="headerlink" title="1.2. StackOverflowError (栈溢出)"></a>1.2. StackOverflowError (栈溢出)</h3><div class="hljs code-wrapper"><pre><code class="hljs Java">java.lang.StackOverflowError</code></pre></div>

<h3 id="1-3-PermGen-space-元空间耗尽"><a href="#1-3-PermGen-space-元空间耗尽" class="headerlink" title="1.3. PermGen space (元空间耗尽)"></a>1.3. PermGen space (元空间耗尽)</h3><div class="hljs code-wrapper"><pre><code class="hljs Java">Java.lang.OutOfMemoryError: PermGen space</code></pre></div>

<h3 id="1-4-Java-heap-space-堆内存溢出"><a href="#1-4-Java-heap-space-堆内存溢出" class="headerlink" title="1.4. Java heap space (堆内存溢出)"></a>1.4. Java heap space (堆内存溢出)</h3><div class="hljs code-wrapper"><pre><code class="hljs Java">java.lang.OutOfMemoryError: Java heap space</code></pre></div>

<h3 id="1-5-Direct-buffer-memory-用来-nio-，但是-direct-buffer-不够"><a href="#1-5-Direct-buffer-memory-用来-nio-，但是-direct-buffer-不够" class="headerlink" title="1.5. Direct buffer memory (用来 nio ，但是 direct buffer 不够)"></a>1.5. Direct buffer memory (用来 nio ，但是 direct buffer 不够)</h3><div class="hljs code-wrapper"><pre><code class="hljs Java">java.lang.OutOfMemoryError: Direct buffer memory</code></pre></div>

<h2 id="2-OOM-定位"><a href="#2-OOM-定位" class="headerlink" title="2. OOM 定位"></a>2. OOM 定位</h2><h3 id="2-1-GC-overhead-limit-exceeded"><a href="#2-1-GC-overhead-limit-exceeded" class="headerlink" title="2.1. GC overhead limit exceeded"></a>2.1. GC overhead limit exceeded</h3><div class="hljs code-wrapper"><pre><code class="hljs Java"><span class="hljs-comment">/**</span>
<span class="hljs-comment"> * jvm参数配置</span>
<span class="hljs-comment"> * -Xms40m  最小堆</span>
<span class="hljs-comment"> * -Xmx40m  最大堆</span>
<span class="hljs-comment"> * -XX:+HeapDumpOnOutOfMemoryError 堆异常输出dump文件</span>
<span class="hljs-comment"> * -XX:HeapDumpPath=D:\360MoveData\Desktop 输出dump文件路径</span>
<span class="hljs-comment"> */</span>
<span class="hljs-meta">@SpringBootApplication</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">XulinglinApplication</span> </span>&#123;

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;
        SpringApplication.run(XulinglinApplication.class, args);
    &#125;

    <span class="hljs-comment">//初始化60w数据到HashMap中</span>
    <span class="hljs-meta">@Bean</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> HashMap <span class="hljs-title">hashMapdump</span><span class="hljs-params">()</span></span>&#123;
        HashMap hashMap = <span class="hljs-keyword">new</span> HashMap();
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt;<span class="hljs-number">600000</span> ; i++) &#123;
            hashMap.put(i,i);
        &#125;
        <span class="hljs-keyword">return</span> hashMap;
    &#125;
&#125;</code></pre></div>

<h3 id="2-2-异常错误log日志"><a href="#2-2-异常错误log日志" class="headerlink" title="2.2. 异常错误log日志"></a>2.2. 异常错误log日志</h3><div class="hljs code-wrapper"><pre><code class="hljs Java">Caused by: java.lang.OutOfMemoryError: GC overhead limit exceeded
    at java.util.HashMap.newNode(HashMap.java:<span class="hljs-number">1734</span>) ~[na:<span class="hljs-number">1.8</span><span class="hljs-number">.0_91</span>]
    at java.util.HashMap.putVal(HashMap.java:<span class="hljs-number">630</span>) ~[na:<span class="hljs-number">1.8</span><span class="hljs-number">.0_91</span>]
    at java.util.HashMap.put(HashMap.java:<span class="hljs-number">611</span>) ~[na:<span class="hljs-number">1.8</span><span class="hljs-number">.0_91</span>]
    at com.xulinglin.XulinglinApplication.hashMapdump(XulinglinApplication.java:<span class="hljs-number">20</span>) [classes/:na]
    at com.xulinglin.XulinglinApplication$$EnhancerBySpringCGLIB$$a8f9ee58.CGLIB$hashMapdump$<span class="hljs-number">0</span>() ~[classes/:na]
    at com.xulinglin.XulinglinApplication$$EnhancerBySpringCGLIB$$a8f9ee58$$FastClassBySpringCGLIB$$9b30d517.invoke() ~[classes/:na]</code></pre></div>
<p class="note note-primary">
文件名称:java_pid73956.hprof,hprof 可以格式让我们更好的利用第三方插件来定位问题。比如使用 Eclipse IDE
</p>

<h3 id="2-3-Hprof-拖入-Eclipse-IDE分析"><a href="#2-3-Hprof-拖入-Eclipse-IDE分析" class="headerlink" title="2.3. Hprof 拖入 Eclipse IDE分析"></a>2.3. Hprof 拖入 Eclipse IDE分析</h3><p><img src="/essay/OOM1.0/img.png" srcset="/img/loading.gif" lazyload><br><img src="/essay/OOM1.0/img_1.png" srcset="/img/loading.gif" lazyload></p>
<p class="note note-primary">
通过上图,可以基本确定导致内存溢出的代码位置。
</p>

<p><img src="/essay/OOM1.0/img_2.png" srcset="/img/loading.gif" lazyload><br><img src="/essay/OOM1.0/img_3.png" srcset="/img/loading.gif" lazyload></p>
<p class="note note-success">
通过 IDE 分析dump文件,查看栈的信息,查看导致内存溢出的对象或容器里具体存储什么信息来定位具体原因
</p>


            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Java/">Java</a>
                    
                      <a class="hover-with-bg" href="/tags/OOM/">OOM</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/06/04/java/HashMap/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Java HashMap原理以及常见的面试题</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/06/04/jvm/jvm2.0/">
                        <span class="hidden-mobile">Java 虚拟机 Jvm 内存模型</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
              <!-- Comments -->
              <article class="comments" id="comments" lazyload>
                
                  
                
                
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://cdn.jsdelivr.net/gh/HCLonely/Valine@latest/dist/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"BmWJPSifebgqliaA9sdhmII7-gzGzoHsz","appKey":"HvFu44h5vVRRYuQIg6uNJkKR","placeholder":"说点什么","path":"window.location.pathname","avatar":"retro","meta":["nick","mail","link"],"pageSize":10,"lang":"zh-CN","highlight":true,"recordIP":false,"serverURLs":"https://bmwjpsif.lc-cn-n1-shared.com","emojiCDN":null,"emojiMaps":null,"enableQQ":false,"requiredFields":[]},
          {
            el: "#valine",
            path: window.location.pathname,
            master: "",
            friends: "",
            tagMeta: ["博主","友人","访客"]
          }
        )
        new Valine(options);
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


              </article>
            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>














  
<script src="/js/diy/timeDate.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
